package com.ruoyi.system.service.impl;

import com.ruoyi.common.config.oss.OssProperties;
import com.ruoyi.common.config.oss.OssTemplate;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysDangerKeep;
import com.ruoyi.system.domain.SysDangerNode;
import com.ruoyi.system.mapper.SysDangerNodeMapper;
import com.ruoyi.system.service.ISysDangerNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysDangerNodeServiceImpl implements ISysDangerNodeService {

    @Autowired
    private OssTemplate template;

    @Autowired
    private OssProperties ossProperties;

    @Autowired
    private SysDangerNodeMapper sysDangerNodeMapper;

    @Override
    public void create(SysDangerNode sysDangerNode) {
        sysDangerNode.setCreateUserId(SecurityUtils.getUserId().intValue());
        sysDangerNode.setCreateUserName(SecurityUtils.getUsername());
        sysDangerNode.setUpdateUserName(SecurityUtils.getUsername());
        sysDangerNodeMapper.create(sysDangerNode);
    }

    @Override
    public List<SysDangerNode> list(SysDangerNode sysDangerNode) {
        return sysDangerNodeMapper.list(sysDangerNode);
    }

    @Override
    public void delete(Integer id) {
        SysDangerNode sysDangerNode = sysDangerNodeMapper.getById(id);
        String fileName = sysDangerNode.getFileName();
        if (StringUtils.isNotNull(fileName)) {
            try {
                template.removeObject(ossProperties.getBucketName(),fileName);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        sysDangerNodeMapper.delete(id);
    }

    @Override
    public void update(SysDangerNode sysDangerNode) {
        sysDangerNode.setUpdateUserName(SecurityUtils.getUsername());
        sysDangerNodeMapper.update(sysDangerNode);
    }

    @Override
    public void deleteFile(Integer id) {
        String username = SecurityUtils.getUsername();
        sysDangerNodeMapper.updateFileColumn(id,username);
    }
}
